import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { toEntity } from "src/utils/dto2Entity";
import { Repository } from "typeorm";
import { DeptDto } from "./dto/dept.dto";
import { DeptEntity } from "./entities/dept.entity";
import { TenantService } from "@/modules/tenant/tenant.service";

@Injectable()
export class DeptService {
  constructor(private readonly tenantService: TenantService) {}
  @InjectRepository(DeptEntity)
  private readonly deptRepository: Repository<DeptEntity>;

  /**
   * 获取部门分页列表
   * @param searchDeptDto 搜索dto
   */
  async getDeptByPId(tenantId: number, pId: string) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    const entities = await tenantRepository.find({
      where: {
        pId,
      },
    });
    return entities;
  }

  /**
   * 获取部门列表
   */
  async getDeptList(tenantId: number) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    const entities = await tenantRepository.find();
    return entities;
  }

  /**
   * 根据部门id获取部门信息
   * @param id 编号
   */
  async getDeptById(tenantId: number, id: number) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    const entity = await tenantRepository.findOne({
      where: {
        id,
      },
    });
    return entity;
  }

  /**
   * 根据部门名称获取部门信息
   * @param deptName 部门名称
   */
  async getDeptByDeptName(tenantId: number, deptName: string) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    const entity = await tenantRepository.findOneBy({
      deptName,
    });
    return entity;
  }

  /**
   * 创建部门
   * @param deptDto 部门dto
   */
  async createDept(tenantId: number, deptDto: DeptDto) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    const dept = await tenantRepository.findOneBy({
      deptName: deptDto.deptName,
    });
    if (dept) {
      throw new HttpException("操作失败,部门名已使用.", HttpStatus.BAD_REQUEST);
    }
    const deptEntity = new DeptEntity();
    toEntity(deptDto, deptEntity);
    deptEntity.createTime = new Date();
    await tenantRepository.insert(deptEntity);
  }

  /**
   * 修改部门
   * @param id 编号
   * @param deptDto 部门dto
   */
  async updateDeptById(tenantId: number, id: number, deptDto: DeptDto) {
    const dept = await this.getDeptById(tenantId, id);
    if (!dept) {
      throw new HttpException(
        "操作失败,未找到部门信息.",
        HttpStatus.BAD_REQUEST
      );
    }
    const deptEntity = new DeptEntity();
    toEntity(deptDto, deptEntity);
    const tenantRepository = await this.getTenantRepository(tenantId);
    await tenantRepository.update(id, deptEntity);
  }

  /**
   * 删除部门
   * @param id 编号
   */
  async removeDeptById(tenantId: number, id: number) {
    const tenantRepository = await this.getTenantRepository(tenantId);
    await tenantRepository.delete(id);
  }
  private async getTenantRepository(
    tenantId: number
  ): Promise<Repository<DeptEntity>> {
    const tenantConnection = await this.tenantService.getTenantConnection(
      tenantId
    );
    return tenantConnection.getRepository(DeptEntity);
  }
}
